﻿using Entity;
using IRepository;
using IService;
using Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
   public class CustomerService: ICustomerService
    {
        private readonly ICustomerRepository _repo;
        public CustomerService(ICustomerRepository repo) => _repo = repo;

        public async Task<int> AddCustomerAsync(WMSBasicCustomer customer)
        {
            return await _repo.AddCustomerAsync(customer);
        }

        public async Task<PagedViewModel<WMS_Basic_Customer2>> GetAllCustomerAsync(string customer_code = null, string customer_name = null, int? status = null, int? job_type = null, int? person_in_charge = null, string contact_person = null, int currentPage = 1, int pageSize = 1)
        {
            // 1. 查询符合条件的全量数据
            List<WMS_Basic_Customer2> wMS_Basic_Customer2s = await _repo.GetAllCustomerAsync(customer_code,
              customer_name, status, job_type, person_in_charge, contact_person);

            // 2. 内存分页（Skip + Take）
            List<WMS_Basic_Customer2> wMS_Basic_Customers = wMS_Basic_Customer2s
                .Skip((currentPage - 1) * pageSize)  // 跳过前N条
                .Take(pageSize)                       // 取当前页数据
                .ToList();

            // 3. 封装分页模型（包含“当前页数据”和“总条数”）
            PagedViewModel<WMS_Basic_Customer2> result = new PagedViewModel<WMS_Basic_Customer2>
            {
                items = wMS_Basic_Customers,  // 修正：与PagedViewModel的属性名保持一致（建议用items）
                total = wMS_Basic_Customer2s.Count    // 总数据条数
            };

            return result; 

        }

        public Task<WMSBasicCustomer> GetCustomerByCode(string customerCode)
        {
           
            return _repo.GetCustomerByCode(customerCode);

        }

        public async Task<WMS_Basic_Customer2> GetCustomerByCodeAsync(string customerCode)
        {
            // 1. 先判断客户编号是否为空
            if (string.IsNullOrWhiteSpace(customerCode))
            {
                // 返回null或抛出特定异常（根据业务需求选择）
                return null; // 推荐返回null，由控制器统一处理提示信息
            }

            // 2. 调用仓储层查询
            return await _repo.GetCustomerByCodeAsync(customerCode);
        }

        public Task<int> UpdateCustomer(WMSBasicCustomer wMS_Basic_Customer)
        {
            return _repo.UpdateCustomer(wMS_Basic_Customer);
        }

        public Task<int> UpdateIsDelete(string customerCode, string isDelete)
        {
            return _repo.UpdateIsDelete(customerCode, isDelete);
        }
    }
}
